package com.yun.util.yun.ip;



import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class L34IP_LOCAL  {

    public String[] getValues( Map<String, ?> arg2, String arg3) {
        Object o = arg2.get("THREEIP");
        if (o == null) {
            return new String[]{"[NULL]"};
        }

        String ip = o.toString().trim();
        if ("".equals(ip)) {
            return new String[]{""};
        }

        // IPV4处理
        if (!ip.contains(":")) {
            return new String[]{getIPV4(ip)};
        }

        //return new String[]{getIPV6(ip)};
        return new String[]{getShortIPv6(ip)};
    }

    private String getIPV4(String ip) {
        String result = "";
        int i = ip.indexOf('/');
        if (i > 0) {
            String ip1 = ip.substring(0, i);
            String ip2 = ip.substring(i + 1);
            result = ip1 + " " + ip2;
        } else {
            String ip1 = ip;
            String ip2 = "32";
            result = ip1 + " " + ip2;
        }
        return result.trim();
    }

    private String getIPV6(String ip) {
        String result = "";
        String mask = "";
        if (ip.contains("/")) {
            mask = ip.split("/")[1];
            ip = ip.split("/")[0];
        }
        String[] arr = ip.split(":");
        String[] arrayOfString1;
        int j = (arrayOfString1 = arr).length;
        for (int i = 0; i < j; i++) {
            String temp = arrayOfString1[i];
            if ((temp.startsWith("0")) && (temp.length() > 1)) {
                temp = temp.substring(1);
                if ((temp.startsWith("0")) && (temp.length() > 1)) {
                    temp = temp.substring(1);
                    if ((temp.startsWith("0")) && (temp.length() > 1)) {
                        temp = temp.substring(1);
                    }
                }
            }
            result = result + temp + ":";
        }
        if (result.endsWith(":")) {
            result = result.substring(0, result.length() - 1);
        }
        if (ip.endsWith("::")) {
            result = result + "::";
        }
        if (result.contains(":0::")) {
            result = result.replace(":0::", "::");
        }

        result = result.replaceAll("(:0+)+", ":");
        //result = result.replaceAll(":0+([^0].*?)", ":$1");

        if (!"".equals(mask)) {
            result = result + " " + mask;
        }

        return result.toLowerCase().trim();
    }

    /**
     * https://www.it1352.com/965382.html
     * https://www.cnblogs.com/luckjinyan/p/13258318.html
     *
     * @param ipv6
     * @return
     */
    public static String getShortIPv6(String ipv6) {
        String shortIP = "";
        ipv6 = getFullIPv6(ipv6);
        String[] arr = ipv6.split(":");

        //去掉每组数据前的0
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i].replaceAll("^0{1,3}", "");//有1到3个0

        }
        //最长的连续0
        String[] arr2 = arr.clone();//复制
        for (int i = 0; i < arr2.length; i++) {
            if (!"0".equals(arr2[i])) {
                arr2[i] = "-";
            }
        }
        Pattern pattern = Pattern.compile("0{2,}");//两个以上的连续0
        Matcher matcher = pattern.matcher(String.join("", arr2));
        String maxStr = "";
        int start = -1;
        int end = -1;
        while (matcher.find()) {
            if (maxStr.length() < matcher.group().length()) {
                maxStr = matcher.group();
                start = matcher.start();
                end = matcher.end();
            }
        }
        // 组合IPv6简写地址
        if (maxStr.length() > 0) {
            for (int i = start; i < end; i++) {
                arr[i] = ":";
            }
        }
        shortIP = String.join(":", arr);
        shortIP = shortIP.replaceAll(":{2,}", "::");
        shortIP = shortIP.replaceAll(":0+:", "::");
        shortIP = shortIP.replaceAll(":0+", "::");
        shortIP = shortIP.replaceAll(":{2,}", "::");
        shortIP = shortIP.replaceAll(": ", "::");
        return shortIP.replace("/", " ");
    }

    public static String getFullIPv6(String ipv6) {
        //入参为::时，此时全为0
        if (ipv6.equals("::")) {
            return "0000:0000:0000:0000:0000:0000:0000:0000";
        }
        //入参已::结尾时，直接在后缀加0
        if (ipv6.endsWith("::")) {
            ipv6 += "0";
        }

        String[] arrs = ipv6.split(":");
        String symbol = "::";
        int arrleng = arrs.length;
        //System.out.println(arrleng);
        while (arrleng < 8) {
            symbol += ":";
            arrleng++;
        }
        ipv6 = ipv6.replace("::", symbol);
        //System.out.println("ipv6:" + ipv6);
        String fullip = "";
        for (String ip : ipv6.split(":")) {
            while (ip.length() < 4) {
                ip = "0" + ip;


            }
            fullip += ip + ':';
        }
        return fullip.substring(0, fullip.length() - 1);
    }


}